-- You may find last version of this script on my web: http://verlihub.dantm.info

botname = "Room101"  -- botname

function Main ()
	watchers = {}
	if (botname ~= "") then
		VH:AddRobot(botname, 10, "+join +part +list", "none", "lolware", "0")
	end
end

function UnLoad ()
	if (botname ~= "") then
		VH:DelRobot(botname)
	end
end

function SendPMToUser(nick,data)
        result, err = VH:SendDataToUser("$To: "..nick.." From: "..botname.." $<"..botname.."> "..data.."|", nick)
        return 1
end 

function SendPMToWatcher(data)
	table.foreach(watchers, function(k,v) SendPMToUser(v,data) end)
end

function commands (nick, data)
    if (string.find(data,"^+join")) then
	addnick(nick)
	return true
    elseif (string.find(data,"^+part")) then
	removenick(nick)
	return true
    elseif (string.find(data,"^+list")) then
	nicklist (nick)
	return true
	end
    return false
end

function VH_OnParsedMsgPM(from, data, to)
	if (to == botname) then
		if commands(from,data) then 
			return 1
		else
		    addnick (from)
			table.foreach(watchers, function(k,v) 
				if (v ~= from) then
					VH:SendDataToUser("$To: "..v.." From: "..botname.." $<"..from.."> "..data.."|", v)
				end
			end)
		end
	end
    	return 1
end

function addnick (nick)
	local je = false
	table.foreach(watchers, function(k,v) 
		if (v == nick) then
			je = true
		end
	end)
	if not je then
		table.insert(watchers,nick)
		SendPMToWatcher("welcome "..nick.." ...")
	end
end

function nicklist (nick)
	local temp = ""
	table.foreach(watchers, function(k,v) temp=temp..v..", " end)
		SendPMToUser(nick, "Those nick are in this room:\r\n "..temp)
end


function removenick (nick)
	   table.foreach(watchers, function(k,v) 
		if (v == nick) then
			SendPMToWatcher(nick.." left...")
			table.remove(watchers,k)
		end
	   end)
end

function VH_OnUserLogout(nick)
    removenick (nick)
    return 1
end